package cn.itcast.user.web;

import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${user.param1}")
    private String param1;

    @Value("${user.param2}")
    private String param2;

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) throws InterruptedException {
        if (id == 1L) {
            Thread.sleep(60);
        } else if (id == 2L) {
            throw new RuntimeException("id == 2");
        }
        return userService.queryById(id);
    }


    @GetMapping("/config")
    public String config() {
        String result = "param1:" + param1 + "\n" +
                        "param2:" + param2;
        return result;
    }



    @Value("${access.key}")
    private String accessKey;

    @GetMapping("/config/share")
    public String configShare() {
        return accessKey;
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/logout")
    public String logout() {
        return "logout";
    }
}
